package com.ruoyi.system.controller;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;

/**
 * @author dingxin
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/ftp")
public class FtpController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

//    @Autowired
//    FtpController(RedisTemplate<String, Object> redisTemplate){
//        this.redisTemplate = redisTemplate;
//    }

    public String getFtpUrl(String fileName) {
        String ftpUrl = "ftp://ftpuser:zdx19981006@150.158.176.170:21/";
        return ftpUrl + fileName;
    }

    /**
     * 版本升级请求
     */
    @Data
    static class VersionRequest{
        private String version;
    }

    private String getNewestVersion(){
        return (String) redisTemplate.opsForValue().get("newest_version");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(@RequestBody VersionRequest message){
        String version = message.version;
        String newVersion = getNewestVersion();
        if(version.equals(newVersion)){
            return RsuController.responseTemplate(400,"Already Updated!");
        }
        return RsuController.responseTemplate(200,getFtpUrl("Version/" + newVersion));
    }

    @RequestMapping(value = "/rollback",method = RequestMethod.POST)
    public String rollBack(@RequestBody VersionRequest message){
        String version = message.version;
        HashSet<String> supportVersion = (HashSet<String>) redisTemplate.opsForValue().get("Supported version");
        if(!supportVersion.contains(version)){
            return RsuController.responseTemplate(404,"Version Not Found");
        }
        return RsuController.responseTemplate(200,getFtpUrl("Version/" + version));
    }

//    @RequestMapping(value = "/test",method = RequestMethod.GET)
//    public void insert(){
//        HashSet<String> version = new HashSet<>();
//        version.add("v1.0");
//        version.add("v1.1");
//        version.add("v1.2");
//        redisTemplate.opsForValue().set("Supported version", version);
//    }

}
